<?php
namespace app\command;

use app\BaseController;
use \think\facade\Request;
use \think\facade\Cache;
use \think\facade\Db;
use \think\facade\Log;

class ComModel extends BaseController
{
  // 普通用户扫码
  static function checkCode1($qr_sn,$user_id)
  {
		$count = Db::table('write_off_history')
        ->where('qr_sn',$qr_sn)
        ->count();
		$arr = [];
		if($count > 0){
			$arr = [
				'type' => '1',
				'msg'	=> '已扫码',
				'aid'	=> '',
			];
			return $arr;
		}

		// award 是奖品表 type不等于6
		 $res1 = Db::table('award')
		 	->field('id,probability,type')
			->where('type','<>','6') // 不等于6和7
			->where('type','<>','7') // 不等于7
			->whereNull('delete_time')
			->select()
			->toArray();
		$res2 = Db::table('award')
		 	->field('id,probability,type')
			->where('type','6') // 等于6
			->whereNull('delete_time')
			->select()
			->toArray();
		// $aids = [];
		// foreach($res as $k=>$v){
		// 	$aids[] = $v;
		// }

		$pid = Db::table('qrcode')
			->where('qr_sn',$qr_sn)
			->value('pid');

		// 随机找一个id 要求这个id 和 pid 不能同时在 write_off_history 如果在的话再随机找一个
		$aid = '';
		// 如果循环50次没有找到符合的 说明数据库有问题 或者 奖品表有问题
		for($i=0;$i<1000;$i++){
			$rand = mt_rand(0,count($res1)-1);
			$award_id = $res1[$rand]['id'];
			$count = Db::table('write_off_history')
			    ->field('id')
				->where('pid',$pid)
				->where('award_id',$award_id)
				->count();
			if($count == 0){
				$aid = $award_id;
				break;
			}
		}
		if($aid == ''){
			// 随机找一个id 要求这个id 和 pid 不能同时在 write_off_history 如果在的话再随机找一个
			$rand = mt_rand(0,count($res2)-1);
			$award_id = $res2[$rand]['id'];
			$aid = $award_id;
		}

		// 内定奖 查询qrcode qr_sn 的aid
		$inner_aid = Db::table('qrcode')
			->where('qr_sn',$qr_sn)
			->value('aid');
		if($inner_aid){
			$aid = $inner_aid;
		}
		
		// 写入 award_history 表
		   $id = Db::table('award_history')
			->insertGetId([
				'uid' => $user_id,
				'aid' => $aid,
				'add_time' => date("Y-m-d H:i:s"),
				'qr_sn' => $qr_sn,
				'order_sn'=>createOrderSn(),
				'pay_order_sn'=>createOrderSn(),
			]);
		if($aid == ''){
			$arr = [
				'type' => '2',
				'msg'	=> '未中奖',
				'aid'	=> '',
			];
		}else{
			$arr = [
				'type' => '3',
				'msg'	=> '已中奖',
				'aid'	=> $id,
			];
		}
		
		Db::table('write_off_history')
		->insert([
			'user_id' => $user_id,
			'add_time' => date("Y-m-d H:i:s"),
			'qr_sn' => $qr_sn,
			'role' => '1',
			'pid' => $pid,
			'award_id' => $aid,
		]);

		// 通过pid 找到箱码的sn
		$qr_sn_p = Db::table('qrcode_p')
			->where('id',$pid)
			->value('qr_sn');
		if($qr_sn_p){
			// 找到write_off_history 中 箱码的状态
			$ywy_user_id = Db::table('write_off_history')
				->where('qr_sn',$qr_sn_p)
				->where('role','2')
				->value('user_id');
			if($ywy_user_id){
				//  修改 order_info 和 award_history
				Db::table('order_info')
					->where('user_id',$user_id)
					->update(['ywy_user_id'=>$ywy_user_id]);
				Db::table('award_history')
					->where('uid',$user_id)
					->update(['ywy_user_id'=>$ywy_user_id]);
			}
		}

		Db::table('qrcode')
			->where('qr_sn',$qr_sn)
			->update(['status'=>'1']);
		return	$arr;
		
  }
  // 普通用户扫码已经作废
   static function __checkCode1($qr_sn,$user_id)
   {
       
	// qr_sn 有没有在 qrcode 表中 在的话返回已核销 不在的话award表 中的 probability概率出1到100的随机数 如果概率是30 中奖概率是30%然后返回中奖的信息
	$count = Db::table('write_off_history')
        ->where('qr_sn',$qr_sn)
        ->count();
	$arr = [];
	if($count > 0){
		$arr = [
			'type' => '1',
			'msg'	=> '已核销',
			'aid'	=> '',
		];
		return $arr;
	}

	  $res = Db::table('award')
        ->where('probability','>',0)
        ->order('probability','desc')
        ->select();
		$rand = mt_rand(1,100);
		$item = '';
		foreach($res as $k=>$v){
			if($rand <= $v['probability']){
				$item = $v;
			}
		}
		if($item == ''){
			$arr = [
				'type' => '2',
				'msg'	=> '未中奖',
				'aid'	=> '',
			];
			
		}else{
			
		   // 写入 award_history 表
		   $aid = Db::table('award_history')
			->insertGetId([
				'uid' => $user_id,
				'aid' => $item['id'],
				'add_time' => date("Y-m-d H:i:s"),
				'qr_sn' => $qr_sn,
			]);
			$arr = [
				'type' => '3',
				'msg'	=> '已中奖',
				'aid'	=> $aid,
			];
		}
		// write_off_history 表 写入
		Db::table('write_off_history')
		->insert([
			'user_id' => $user_id,
			'add_time' => date("Y-m-d H:i:s"),
			'qr_sn' => $qr_sn,
			'role' => '1',
		]);

		

		Db::table('qrcode')
			->where('qr_sn',$qr_sn)
			->update(['status'=>'1']);
		return	$arr;
	    
		
   }
   // 合作方扫码
   static function checkCode2($qr_sn,$user_id)
   {
       
		// qr_sn 有没有在 write_off_history 
		$count = Db::table('write_off_history')
			->where('qr_sn',$qr_sn)
			->where('hzf_hx','1')
			->count();
		$arr = [];
		if($count > 0){
			$arr = [
				'type' => '1',
				'msg'	=> '已扫码',
				'aid'	=> '',
			];
			return $arr;
		}
		// reward_desc 查询出来 奖励金额  reward_price  插入 reward_history 表中
		// $res = Db::table('reward_desc')
		// 	->where('cid',1)
		// 	->find();
		// if($res){
		// 	Db::table('reward_history')
		// 	->insert([
		// 		'user_id' => $user_id,
		// 		'add_time' => date("Y-m-d H:i:s"),
		// 		'qr_sn' => $qr_sn,
		// 		'reward_price' => $res['reward_price'],
		// 		'type'=>'1',
		// 	]);
		// }
		// write_off_history 表 写入
		Db::table('write_off_history')
		->insert([
			'user_id' => $user_id,
			'add_time' => date("Y-m-d H:i:s"),
			'qr_sn' => $qr_sn,
			'role' => '2',
			'hzf_hx'=>'1',
		]);
        
		// 如果 hzf_hx 和 ywy_hx 都为1 则为核销成功 把 qrcode_p 的status改为1
		$count1 = Db::table('write_off_history')
			->where('qr_sn',$qr_sn)
			->where('hzf_hx','1')
			->count();
		$count2 = Db::table('write_off_history')
			->where('qr_sn',$qr_sn)
			->where('ywy_hx','1')
			->count();
		if($count1 > 0 && $count2 > 0){
			Db::table('qrcode_p')
			->where('qr_sn',$qr_sn)
			->update(['status'=>'1']);
		}

		return	['type'=>'4','msg'=>'核销成功','aid'=>''];
		
	    
		
   }
   // 业务员核销
   static function checkCode3($qr_sn,$user_id)
   {
       
		// qr_sn 有没有在 write_off_history 
		$count = Db::table('write_off_history')
			->where('qr_sn',$qr_sn)
			->where('ywy_hx','1')
			->count();
		$arr = [];
		if($count > 0){
			$arr = [
				'type' => '1',
				'msg'	=> '已核销',
			];
			return $arr;
		}
		// write_off_history 表 写入
		Db::table('write_off_history')
		->insert([
			'user_id' => $user_id,
			'add_time' => date("Y-m-d H:i:s"),
			'qr_sn' => $qr_sn,
			'role' => '3',
			'ywy_hx'=>'1',
		]);
		$count1 = Db::table('write_off_history')
			->where('qr_sn',$qr_sn)
			->where('hzf_hx','1')
			->count();
		$count2 = Db::table('write_off_history')
			->where('qr_sn',$qr_sn)
			->where('ywy_hx','1')
			->count();
		if($count1 > 0 && $count2 > 0){
			Db::table('qrcode_p')
			->where('qr_sn',$qr_sn)
			->update(['status'=>'1']);
		}
		return	['type'=>'4','msg'=>'已核销','aid'=>''];
		
   }
}
